home *** CD-ROM | disk | FTP | other *** search
- ================================================================
- Usort.Pas Program in Turbo Pascal 3.0 for the IBM PC and
- compatibles. From the article "Sorting out the Sorts"
- by Dick Pountain, July 1987, page 275.
- =================================================================
-
-
- program USORT;
- const CR = #13; { carriage return character }
- type letters = 'a'..'z';
- wordtype = string[16];
- nodeptr = ^nodetype;
- nodetype = record
- info: wordtype;
- next: nodeptr
- end;
- var inputFile,outputFile: text;
- inputFilename, outputFilename: string[127];
- chr,firstletter: char;
- sortList: array[letters] of nodeptr; { the array of 26 lists }
- i: letters;
- word: wordtype;
- procedure InitFiles;
- begin { open input and output files }
- inputFilename := paramSTR(1);
- Assign(inputFile,inputFilename);
- Reset(inputFile);
- outputFilename := paramSTR(2);
- Assign(outputFile, outputFilename);
- Rewrite(outputFile);
- end;
- procedure GetWord(VAR infile: text; VAR word: wordtype);
- begin { read a cleaned-up word from the input file }
- word := ''; { initialize to blank }
- repeat
- read(infile,chr);
- if chr in ['A'..'Z'] { convert all to lowercase }
- then chr := char(ord(chr)+32);
- if chr in ['a'..'z'] { only accept alpha characters }
- then word := word+chr; { add to word being built }
- until (chr = ' ') or (chr = CR) or eof(infile)
- end;
- procedure Place(VAR list: nodeptr; word: wordtype);
- var p,q,newnode: nodeptr;
- found: boolean;
- begin { insert new word into list in sorted position only if unique }
- q := nil;
- p := list; { p points to head of list }
- found := false;
- while (p <> nil) { not end of list and }
- and (not found) { word not already here and }
- and (word >= p^.info) do { word alphabetically later than current }
- if p^.info = word { does this node contain our word? }
- then found := true { yes! word is already here }
- else begin
- q := p; { remember this node and }
- p := p^.next { move on to the next one }
- end; {while}
- if not found { word isn't already here }
- then begin
- New(newnode); { create a new node }
- newnode^.info := word; { put word in its info field }
- if q = nil { list was empty }
- then begin
- newnode^.next := list; { newnode becomes first }
- list := newnode
- end
- else begin
- newnode^.next := q^.next; { insert after node q }
- q^.next := newnode
- end
- end
- end;
- procedure SquirtOut(list: nodeptr; VAR outfile: text);
- begin { send sorted list to output file }
- while list <> nil
- begin
- writeln(outfile,list^.info);
- list := list^.next
- end
- end;
- begin { main program }
- InitFiles;
- for i := 'a' to 'z' do sortList[i] := nil; { initialize all the lists }
- while not eof(inputFile) do
- begin
- GetWord(inputFile,word);
- firstletter := word[1]; { get first letter }
- Place(sortList[firstletter],word) { put word in proper place }
- end; {while}
- for i := 'a' to 'z' do SquirtOut(sortList[i],outputFile);
- writeln('Keywords are contained in ',outputFilename);
- Close(inputFile);
- Close(outputFile)
- end.!ENDLISTING2!
-
- !CAPTION!Listing 2. USORT.PAS, a text indexing program in Turbo Pascal 3.0. (Compile into a .COM file.)!ENDCAPTION!
-
-